﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Xml.Serialization;

namespace PFMConfigurations
{
    public class ConfigurationManager<T>
    {
        public List<T> Items { get; set; }
        protected string XmlFile { get; set; }
        public void Load()
        {
            if (File.Exists(XmlFile))
            {
                using (var fileStream = File.Open(XmlFile, System.IO.FileMode.Open))
                {
                    var XmlSerializtion = new XmlSerializer(typeof(List<T>));
                    this.Items = (List<T>)XmlSerializtion.Deserialize(fileStream);
                }
            }
            else
            {
                this.Items = new List<T>();
            }
        }
        public int getConfigurationCount()
        {
            //return Items.Count;
            return Items != null ? Items.Count : 0;
        }
        public virtual T GetItem(object id)
        {

            return default(T);
        }

        public bool Save()
        {

            try
            {
                using (var fileStream = File.Open(XmlFile, System.IO.FileMode.Create))
                {
                    var XmlSerializtion = new XmlSerializer(typeof(List<T>));
                    XmlSerializtion.Serialize(fileStream, Items);
                }
                return true;
            }
            catch (Exception e)
            {
                return false;
            }
        }
    }
}
